﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Helpers
{
    public static class SysHelper
    {
        public static void GetIp(out string ip,out string addr)
        {
            ip = string.Empty;
            addr = string.Empty;
            HttpWebRequest request=null;
            try
            {
                request = HttpWebRequest.CreateHttp("http://2019.ip138.com/ic.asp");
                request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7");
                request.Headers.Add("Referer", "http://www.ip138.com/");
                request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
                using (var response = request.GetResponse())
                {
                    using (var sr = new StreamReader(response.GetResponseStream(),Encoding.GetEncoding("gb2312")))
                    {
                        var html = sr.ReadToEnd();
                        var match = Regex.Match(html, @"<center>您的IP是：\[(.*?)\] 来自：(.*?)</center>");
                        if (match.Success)
                        {
                            ip = match.Groups[1].Value;
                            addr = match.Groups[2].Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                request?.Abort();
            }
        }

    }
}
